package com.totcy.atom.view.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.animation.DecelerateInterpolator;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.totcy.atom.R;
import com.totcy.atom.adapter.GankTextAdapter;
import com.totcy.atom.bean.GankTypeBean;
import com.totcy.atom.presenter.GankTypeListPresenter;
import com.totcy.atom.presenter.contract.GankTypeListContract;
import com.totcy.atom.view.activity.WebViewActivity;
import com.totcy.baselibrary.adapter.BaseLoadMoreAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * description : gank新闻数据
 *
 * @author : Totcy
 * @date : 2018/2/8 15:33
 * @update : 2018/10/25
 */

public class GankNewsFragment extends BaseFragment<GankTypeListContract.View, GankTypeListPresenter> implements GankTypeListContract.View, BaseLoadMoreAdapter.OnItemClickListener, OnRefreshListener, OnLoadmoreListener {


    @BindView(R.id.recyclerView)
    RecyclerView rvImage;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;

    private int pageNum = 1;
    private static final int pageSize = 15;
    private GankTextAdapter adapter;
    private List<GankTypeBean> list = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.fragment_gank_news;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
//        LinearLayoutManager lm = new LinearLayoutManager(getActivity());
//        lm.setOrientation(LinearLayoutManager.VERTICAL);

        StaggeredGridLayoutManager lm = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        rvImage.setLayoutManager(lm);
        rvImage.setLayoutManager(lm);

        //banner set
        adapter = new GankTextAdapter(getActivity(), list);
        rvImage.setAdapter(adapter);

        //adapter.setOnLoadMoreListener(rvVideo, this);
        //关闭adapterde加载更多功能
        adapter.closeLoadMore();

        adapter.setOnItemClickListener(this);
        //refresh.setPrimaryColorsId(R.color.white);
        //设置回弹动画插值器 减速效果
        refresh.setReboundInterpolator(new DecelerateInterpolator());
        refresh.setOnRefreshListener(this);
        refresh.setEnableLoadmore(true);
        refresh.setOnLoadmoreListener(this);
        refresh.setEnableAutoLoadmore(true);
        //自动刷新
        refresh.autoRefresh(200);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        pageNum = 1;
        getPresenter().getGankTypeData(getFragmentTitle(), pageSize, pageNum);
    }
    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        pageNum++;
        getPresenter().getGankTypeData(getFragmentTitle(), pageSize, pageNum);
    }

    @Override
    public void onGankTypeData(List<GankTypeBean> images) {
        refresh.finishRefresh(true);
        refresh.finishLoadmore();
        if (pageNum == 1) {
            list.clear();
        }
        list.addAll(images);
        adapter.notifyDataSetChanged();
        refresh.setLoadmoreFinished(images.size() != pageSize);
    }

    @Override
    public void onDataFail() {
        //adapter.setLoadFail();
        refresh.finishRefresh(false);
        refresh.finishLoadmore(false);
    }

    @Override
    public void onItemClick(int position) {
        Bundle bundle = new Bundle();
        bundle.putString("url", list.get(position).getUrl());
        bundle.putString("title", list.get(position).getDesc());
        if (list.get(position).getImages() != null && list.get(position).getImages().size() > 0) {
            bundle.putString("thumbUrl", list.get(position).getImages().get(0));
        }
        openActivity(WebViewActivity.class, bundle);
    }

}
